package com.chart.chart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartConfigsUser;
import com.chart.chart.domian.ChartViewsUser;
import com.chart.chart.domian.vo.CharIdsVO;
import com.chart.chart.domian.vo.ChartConfigsInterface;
import com.chart.chart.mapper.ChartConfigsMapper;
import com.chart.chart.mapper.ChartConfigsUserMapper;
import com.chart.chart.mapper.ChartViewsMapper;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.config.ChartConfig;
import com.chart.common.utils.SecurityUtils;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.chart.common.utils.PageUtils.startPage;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class ChartConfigsServiceImpl extends ServiceImpl<ChartConfigsMapper, ChartConfigs> implements IChartConfigsService {
    @Autowired
    private ChartConfigsMapper chartConfigsMapper;

    @Override
    public List<ChartConfigs> getList(ChartConfigs chartConfigs) {
//        startPage();
        List<ChartConfigs> list = chartConfigsMapper.getList(chartConfigs);
        return list;
    }

    @Override
    public List<ChartConfigs> getBriefList(Long id) {
        return chartConfigsMapper.getBriefList(id);
    }

    @Override
    public List<ChartConfigsInterface> getChartInterfaces() {
        return chartConfigsMapper.getChartInterfaces();
    }


    @Autowired
    private ChartConfigsMapper chartConfigMapper;

    @Autowired
    private ChartConfigsUserMapper chartConfigsUserMapper;
    @Autowired
    private ChartViewsMapper chartViewsMapper;
    public List<ChartConfigs> getBriefListByUserId(Long chartTypeId, Long userId) {


        // 根据 chartTypeId 和 userId 查询 chart_configs_user 表中的配置
        LambdaQueryWrapper<ChartConfigsUser> eq = new LambdaQueryWrapper<ChartConfigsUser>()
                .eq(ChartConfigsUser::getChartType, chartTypeId)
                .eq(ChartConfigsUser::getUserId, userId);


        List<ChartConfigsUser> userConfigs = chartConfigsUserMapper.selectList(eq);

        // 将用户配置转化为 ChartConfigs 列表
        List<ChartConfigs> chartConfigsList = new ArrayList<>();

        for (ChartConfigsUser userConfig : userConfigs) {

            ChartConfigs chartConfig = new ChartConfigs();
            chartConfig.setId(userConfig.getConfigId());
            chartConfig.setChartTypeId(Long.valueOf(userConfig.getChartType()));
            // 其他属性的转换和设置
            chartConfigsList.add(chartConfig);

        }
        return chartConfigsList;
    }


    /**
     * 查询用户保存到我的生成的数量
     *
     * @return
     */
    @Override
    public Long queryChartCountById() {
        Long userId = SecurityUtils.getUserId();
        Long chartNumber = chartConfigsUserMapper.queryChartCountById(userId);
        return chartNumber;
    }




    /**
     * 根据id查询我的生成表和大屏表
     * @param
     * @return
     */
    @Override
    public CharIdsVO chartConfigsUserAndChartViewsUser(List<Long> configIds, Long viewId) {
        // 查询所有指定ID的ChartConfigsUser记录
        List<ChartConfigsUser> configsUserList = chartConfigsUserMapper.selectBatchIds(configIds);

        // 根据viewId查询单个ChartViewsUser记录
        ChartViewsUser chartViewsUser = chartViewsMapper.selectById(viewId);

        // 创建CharIdsVO并设置属性
        CharIdsVO charIdsVO = new CharIdsVO();
        charIdsVO.setConfigsUserList(configsUserList);
        charIdsVO.setChartViewsUser(chartViewsUser);

        return charIdsVO;
    }


}
